package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Member;
import com.itheima.health.pojo.Order;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.pojo.TPhysicalexamrecord;
import com.itheima.health.service.*;
import com.itheima.health.vo.OrderInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Transactional
@Slf4j
@RestController
@RequestMapping("mobile/order")
public class OrderController {
    @DubboReference
    private OrderService orderService;
 /*   @DubboReference
    private TSaveItemgroupService itemgroupService;*/
    @DubboReference
    private MemberService memberService;
    @DubboReference
    private SendCodeService sendCodeService;
    @DubboReference
    private TPhysicalexamrecordService tPhysicalexamrecordService;
    @DubboReference
    private SetMealService setMealService;

    @PostMapping("submit")
    public Result submit(@RequestBody OrderInfo orderInfo) {
        //验证码校验
        Boolean check = sendCodeService.checkcode("ORDER", orderInfo.getTelephone(), orderInfo.getValidateCode());
        if (!check) {//验证码校验未通过
            return new Result(false, MessageConst.VALIDATECODE_ERROR);
        }
        Member memberServiceByTele = memberService.findByTele(orderInfo.getTelephone());
        if (memberServiceByTele==null) {
            //验证码验证通过
            //用户信息维护
            Member member = new Member();
            member.setPhoneNumber(orderInfo.getTelephone());
            member.setName(orderInfo.getName());
            member.setIdCard(orderInfo.getIdCard());
            member.setSex(orderInfo.getSex());
            member.setBirthday(orderInfo.getBirthday());
            member.setRegTime(new Date());
            member = memberService.addIsNotEx(member);
            //订单信息维护
            Order order = new Order();
            order.setMemberId(member.getId());
            order.setOrderDate(orderInfo.getOrderDate());
            order.setSetmealId(orderInfo.getSetmealId());
            order.setOrderStatus(Order.ORDERSTATUS_WAIT);
            order.setOrderType(Order.ORDERTYPE_WEIXIN);
            order.setName(orderInfo.getName());
            order.setPhoneNumber(orderInfo.getTelephone());
            order = orderService.add(order);
            return new Result(true, MessageConst.ACTION_SUCCESS, order);
        }else {
            Integer id = memberServiceByTele.getId();
            Member member = memberService.findById(id);
            //订单信息维护
            Order order = new Order();
            order.setMemberId(member.getId());
            order.setOrderDate(orderInfo.getOrderDate());
            order.setSetmealId(orderInfo.getSetmealId());
            order.setOrderStatus(Order.ORDERSTATUS_WAIT);
            order.setOrderType(Order.ORDERTYPE_WEIXIN);
            order.setName(member.getName());
            order.setPhoneNumber(member.getPhoneNumber());
            order = orderService.add(order);
            return new Result(true,MessageConst.ACTION_SUCCESS,order);
        }
    }

    @GetMapping("findById")
    public Result findById(Integer id) {
        Map<String, Object> map = orderService.findById(id);
//        Map<String,Object> itemgroup = itemgroupService.findById(id);
        return new Result(true, MessageConst.QUERY_ORDER_SUCCESS, map);
    }

    @GetMapping("findOrderById")
    public Result findOrderById(Integer id) {
        Order order = orderService.findOrderById(id);
//        Map<String,Object> itemgroup = itemgroupService.findById(id);
        return new Result(true, MessageConst.QUERY_ORDER_SUCCESS, order);
    }

    @GetMapping("findTomMember")
    public Result findTomMember(){
       return new Result(true,MessageConst.ACTION_SUCCESS,orderService.findTomMember());
    }

    @GetMapping("findpage")
    public Result finaPage(QueryPageBean queryPageBean) {
        PageResult page = orderService.findPage(queryPageBean);
        log.info("order:findpage,data{}:",page);
        return new Result(true, MessageConst.QUERY_ORDER_SUCCESS, page);
    }

    /**
     * @Author: lenovo
     * @Date: 23:17 2021/5/11
     * @Parms [id, orderStatus]
     * @ReturnType: com.itheima.health.entity.Result
     * @Description: 修改预约状态为驳回
    */
    @PostMapping("deleteOSL")
    public Result delete(Integer id) {
        String status=Order.ORDERSTATUS_reject;
        orderService.delete(id,status);
        return new Result(true, MessageConst.ACTION_SUCCESS);
    }

    /**
     * @Author: lenovo
     * @Date: 8:10 2021/5/12
     * @Parms [id, orderStatus]
     * @ReturnType: com.itheima.health.entity.Result
     * @Description: 修改续约状态为待体检
    */
    @PostMapping("editOSL")
    public Result edit(Integer id) {
        orderService.edit(id,Order.ORDERSTATUS_YES);
        return new Result(true, MessageConst.EDIT_MEMBER_SUCCESS);
    }

    @PostMapping("editDJZ")
    public Result editDJZ(Integer id) {
//        orderStatus: "报告未上传"}
        orderService.edit(id,Order.ORDERREPORT_NO);

        //        维护体检报告表
        Order orderById = orderService.findOrderById(id);
        Integer memberId = orderById.getMemberId();
        Integer setmealId = orderById.getSetmealId();
        Member member = memberService.findById(memberId);
        String phoneNumber = member.getPhoneNumber();
        String name = member.getName();
        String fileNumber = member.getFileNumber();
        Setmeal setmeal = setMealService.findById(setmealId);
        String setmealName = setmeal.getName();

        TPhysicalexamrecord tPhysicalexamrecord=new TPhysicalexamrecord();
        tPhysicalexamrecord.setMemberId(memberId);
        tPhysicalexamrecord.setName(name);
        tPhysicalexamrecord.setPhysicalexamtime(new Date());
        tPhysicalexamrecord.setFilenumber(fileNumber);
        tPhysicalexamrecord.setPhonenumber(phoneNumber);
        tPhysicalexamrecord.setSetmeal(setmealName);
        tPhysicalexamrecordService.add(tPhysicalexamrecord,Order.ORDERREPORT_NO);
        return new Result(true, MessageConst.EDIT_MEMBER_SUCCESS);
    }

    @GetMapping("findAll")
    public Result findAll() {
        List<Order> all = orderService.findAll();
        return new Result(true, MessageConst.QUERY_ORDER_SUCCESS, all);
    }

}
